local jiaoxi = fk.CreateSkill {
  name = "lingling__jiaoxi",
}

Fk:loadTranslationTable{
  ["lingling__jiaoxi"] = "狡袭",
  [":lingling__jiaoxi"] = "出牌阶段各限一次，你可以将一张【杀】当【顺手牵羊】/锦囊牌当【杀】使用。以此法使用的牌无距离与次数限制。若对本回合有摸牌/弃置牌的角色使用，你摸一张牌。",

  ["#lingling__jiaoxi-snatch"] = "狡袭：将一张【杀】当【顺手牵羊】使用",
  ["#lingling__jiaoxi-slash"] = "狡袭：将一张锦囊牌当【杀】使用",
  ["#lingling__jiaoxi"] = "狡袭：将【杀】当【顺手牵羊】使用，或将锦囊牌当【杀】使用",

  ["lingling__jiaoxi_draw"] = "可摸牌",
}

jiaoxi:addEffect("viewas", {
  anim_type = "offensive",
  mute_card = false,
  prompt = function (self, player, selected_cards, selected)
    if #selected_cards > 0 then
      local name = "slash"
      if Fk:getCardById(selected_cards[1]).trueName == "slash" then
        name = "snatch"
      end
      return "#lingling__jiaoxi-"..name
    else
      return "#lingling__jiaoxi"
    end
  end,
  --[[interaction = function(self, player)
    local all_names = {"snatch", "slash"}
    local names = player:getViewAsCardNames(jiaoxi.name, all_names, nil, player:getTableMark("lingling__jiaoxi-phase"))
    if #names == 0 then return end
    return UI.CardNameBox { choices = names, all_choices = all_names }
  end,]]--
  handly_pile = true,
  card_filter = function(self, player, to_select, selected)
    local card = Fk:getCardById(to_select)
    if #selected == 0 then
      if card.trueName == "slash" and not table.contains(player:getTableMark("lingling__jiaoxi-phase"), "snatch") then
        return true
      end
      if card.type == Card.TypeTrick and not table.contains(player:getTableMark("lingling__jiaoxi-phase"), "slash") then
        return true
      end
    end
  end,
  view_as = function(self, player, cards)
    if #cards ~= 1 then return end
    local name = "slash"
    if Fk:getCardById(cards[1]).trueName == "slash" then
      name = "snatch"
    end
    if table.contains(player:getTableMark("lingling__jiaoxi-phase"),name) then return end
    local card = Fk:cloneCard(name)
    card.skill = Fk.skills["lingling_jiaoxi__"..name.."_skill"]
    card.skillName = jiaoxi.name
    card:addSubcard(cards[1])
    return card
  end,
  before_use = function (self, player, use)
    use.extraUse = true
    local room = player.room
    room:addTableMark(player, "lingling__jiaoxi-phase", use.card.trueName)
    for _, cp in ipairs(use.tos) do
      if use.card.trueName == "snatch" and #room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
        for _, move in ipairs(e.data) do
          if move.to == cp and move.moveReason == fk.ReasonDraw then
            return true
          end
        end
      end, Player.HistoryTurn) > 0 then
        use.extra_data = use.extra_data or {}
        use.extra_data.lingling__jiaoxi_draw = true
      end
      if use.card.trueName == "slash" and #room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
        for _, move in ipairs(e.data) do
          if move.from == cp and move.moveReason == fk.ReasonDiscard and table.find(move.moveInfo, function(info)
              return info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip
            end) then
            return true
          end
        end
      end, Player.HistoryTurn) > 0 then
        use.extra_data = use.extra_data or {}
        use.extra_data.lingling__jiaoxi_draw = true
      end
    end
  end,
  after_use = function (self, player, use)
    if use.extra_data and use.extra_data.lingling__jiaoxi_draw then
      player:drawCards(1,jiaoxi.name)
    end
  end,
  enabled_at_play = function(self, player)
    return #player:getViewAsCardNames(jiaoxi.name, {"snatch", "slash"}, nil, player:getTableMark("lingling__jiaoxi-phase")) > 0
  end,
})

--[[jiaoxi:addEffect("prohibit", {
  is_prohibited = function (self, from, to, card)
    if card and to and table.contains(card.skillNames, jiaoxi.name) then
      if card.trueName == "snatch" then
        return table.find(Fk:currentRoom().alive_players, function (p)
          return p:getHandcardNum() > to:getHandcardNum()
        end)
      elseif card.trueName == "slash" then
        return not to:isKongcheng()
      end
    end
  end,
})]]--

jiaoxi:addEffect("targetmod", {
  bypass_times = function (self, player, skill, scope, card, to)
    return card and to and table.contains(card.skillNames, jiaoxi.name)
  end,
  bypass_distances = function(self, player, skill, card, to)
    return card and table.contains(card.skillNames, jiaoxi.name)
  end,
})

jiaoxi:addEffect(fk.AfterCardsMove,{
  can_refresh = function (self, event, target, player, data)
    return true
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    for index, cp in ipairs(room.alive_players) do
      if cp:getMark("lingling__jiaoxi_draw-turn") == 0 then
        if #room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
          for _, move in ipairs(e.data) do
            if move.to == cp and move.moveReason == fk.ReasonDraw then
              return true
            end
          end
        end, Player.HistoryTurn) > 0 then
          room:setPlayerMark(cp,"lingling__jiaoxi_draw-turn",1)
        end
      end
      if cp:getMark("lingling__jiaoxi_discard-turn") == 0 then
        if #room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
          for _, move in ipairs(e.data) do
            if move.from == cp and move.moveReason == fk.ReasonDiscard and table.find(move.moveInfo, function(info)
                return info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip
            end) then
              return true
            end
          end
        end, Player.HistoryTurn) > 0 then
          room:setPlayerMark(cp,"lingling__jiaoxi_discard-turn",1)
        end
      end
    end
  end,
})

jiaoxi:addAI(nil, "vs_skill")

return jiaoxi
